﻿Imports System.Linq
Imports System.Data
Imports System.Web
Imports Equipment.DataAccess

Partial Public Class AssetStatus

#Region "Properties"
    Private _db As EquipmentDataContext
    Public Property DBContext As EquipmentDataContext
        Get
            Return _db
        End Get
        Set(ByVal value As EquipmentDataContext)
            _db = value
        End Set
    End Property

#End Region

    Public Shared Function RetrieveAll() As IEnumerable(Of AssetStatus)
        Dim db As EquipmentDataContext = Database.Instance
        Return db.AssetStatus.OrderBy(Function(s) s.Description)
    End Function

    Public Shared Function GetByPrimaryKey(ByVal id As Integer) As AssetStatus
        Dim _dataBase As EquipmentDataContext = Database.Instance
        Dim result = From at In _dataBase.AssetStatus Where at.ID = id
        Dim assetStatusEntity As AssetStatus = result.FirstOrDefault()
        assetStatusEntity.DBContext = _dataBase
        Return assetStatusEntity
    End Function

    Public Shared Function GetByStatusName(ByVal name As String) As AssetStatus
        Dim _dataBase As EquipmentDataContext = Database.Instance
        Dim result = From at In _dataBase.AssetStatus Where at.Description = name
        Dim assetStatusEntity As AssetStatus = result.FirstOrDefault()
        If assetStatusEntity IsNot Nothing Then
            assetStatusEntity.DBContext = _dataBase
        End If
        Return assetStatusEntity
    End Function

    Public Shared Function Insert(ByVal description As String) As AssetStatus

        Dim _dataBase As EquipmentDataContext = Database.Instance
        Dim newStatus As New AssetStatus

        newStatus.Description = description

        _dataBase.AssetStatus.InsertOnSubmit(newStatus)
        Try
            _dataBase.SubmitChanges()

        Catch ex As Exception
            ' logger.Error(exception.Message + ":" + exception.StackTrace);
        End Try
        Return newStatus
    End Function


    Public Shared Sub Delete(ByVal id As Integer)

        Dim _dataBase As EquipmentDataContext = Database.Instance
        Dim query = From o In _dataBase.AssetStatus Where o.ID = id
        Dim deletedEntity As AssetStatus = query.SingleOrDefault()

        _dataBase.AssetStatus.DeleteOnSubmit(deletedEntity)

        Try
            _dataBase.SubmitChanges()

        Catch ex As Exception
            ' logger.Error(exception.Message + ":" + exception.StackTrace);
        End Try

    End Sub

    Public Sub Save()
        If Me.DBContext IsNot Nothing Then
            Dim _dataBase As EquipmentDataContext = Me.DBContext

            Try
                _dataBase.SubmitChanges()
            Catch ex As Exception
                ' logger.Error(exception.Message + ":" + exception.StackTrace);
            End Try
        Else
            Save(Me)
        End If

    End Sub

    Public Sub Save(ByVal status As AssetStatus)
        Dim statusToUpdate As AssetStatus = AssetStatus.GetByPrimaryKey(status.ID)
        statusToUpdate.Description = status.Description
        statusToUpdate.IsActive = status.IsActive
        statusToUpdate.Save()
    End Sub


End Class
